import { query, remove, add, edit, fetchEdit } from '@/services/bestForm';

export default {
  namespace: 'qualityTest',

  state: {
    data: {
      list: [],
      pagination: {},
    }, // 绑定页数据
    pagedata: {
      list: [],
      pagination: {},
    }, // 报告页数据
    listData: [], // 绑定页选项数据
    detail: [],
    vehicle:{}, //车辆数据
    loading: false,
    editData: {},
    options: [],
  },

  effects: {
    *fetch({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: true },
      });
      const response = yield call(query, payload);
      yield put({
        type: 'save',
        payload: { data: response, loading: false },
      });
      if (callback) callback(response);
    },
    *fetchPage({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: true },
      });
      const response = yield call(query, payload);
      yield put({
        type: 'save',
        payload: { pagedata: response, loading: false },
      });
      if (callback) callback(response);
    },
    *detail({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: true },
      });
      const response = yield call(fetchEdit, payload);
      yield put({
        type: 'save',
        payload: { detail: response, loading: false },
      });
      if (callback) callback(response);
    },
    *list({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: true },
      });
      const response = yield call(query, payload);
      yield put({
        type: 'save',
        payload: { listData: response, loading: false },
      });
      if (callback) callback(response);
    },
    *add({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: true },
      });
      const response = yield call(add, payload);
      yield put({
        type: 'save',
        payload: { data: response, loading: false },
      });
      if (callback) callback(response);
    },
    *fetchEdit({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: true },
      });
      const response = yield call(fetchEdit, payload);
      yield put({
        type: 'save',
        payload: { editData: response, loading: false },
      });
      if (callback) callback(response);
    },
    *edit({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: true },
      });
      const response = yield call(edit, payload);
      yield put({
        type: 'save',
        payload: { data: response, loading: false },
      });
      if (callback) callback(response);
    },
    *getVehicle({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: true },
      });
      const response = yield call(query, payload);
      yield put({
        type: 'save',
        payload: { vehicle: response, loading: false },
      });
      if (callback) callback(response);
    },
    *remove({ payload, callback }, { call }) {
      const response = yield call(remove, payload);
      if (callback) callback(response);
    },
    *options({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: true },
      });
      let response = yield call(query, payload);
      
      if(!Array.isArray(response)){
        response = [];
      }

      yield put({
        type: 'save',
        payload: { options: response, loading: false },
      });
      if (callback) callback(response);
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
